from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,BadData
from mall import settings

def generate_safe_token(openid):
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'openid':openid}
    safe_token = serializer.dumps(data)
    return safe_token.decode()

def check_user_token(access_token):
    serializer = Serializer(settings.SECRET_KEY)
    try:
        openid_dict = serializer.loads(access_token)
        openid = openid_dict['openid']
    except BadData:
        return  None
    else:
        return openid
